package Pre.Work;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class Exaam02 {

    //创建一个HashSet，包含一些日期对象 (java.util.Date)。编写代码，找到集合中的最早日期和最晚日期
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(LocalDate.of(2002, 2, 16));
        hashSet.add(LocalDate.of(2002, 8, 20));
        hashSet.add(LocalDate.of(2001, 9, 8));
        hashSet.add(LocalDate.of(2002, 2, 19));
        hashSet.add(LocalDate.of(1999, 10, 26));
        hashSet.add(LocalDate.of(2012, 4, 16));
        hashSet.add(LocalDate.of(2022, 3, 25));

        TreeSet treeSet = new TreeSet(hashSet);
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("最早的日期：" + treeSet.first());
        System.out.println("最晚的日期：" + treeSet.last());


        HashSet<Date> hashSet1 = new HashSet<>();
        hashSet1.add(new Date(49, 9, 1));
        hashSet1.add(new Date(50, 9, 1));
        hashSet1.add(new Date(51, 9, 1));
        hashSet1.add(new Date(123, 8, 8));
        hashSet1.add(new Date(124, 8, 9));
//        hashSet1.forEach(o -> System.out.println(o));
        Date first = new Date();
        Date last = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Iterator<Date> iterator1 = hashSet1.iterator();
        while (iterator1.hasNext()) {
            Date d = iterator1.next();
            if (first.compareTo(d) > 0) {
                first = d;
            }
            if (last.compareTo(d) < 0) {
                last = d;
            }
        }
        System.out.println(sdf.format(first));
        System.out.println(sdf.format(last));

//        TreeSet<Date> dateTreeSet = new TreeSet<>(hashSet);
//        Date first = dateTreeSet.first();
//        Date last = dateTreeSet.last();
//
//        System.out.println(sdf.format(first));
//        System.out.println(sdf.format(last));
//        System.out.println(dateTreeSet);
//        dateTreeSet.addAll(hashSet);
    }
}
